//
//  FridUtil.m
//  SignSystem
//
//  Created by Chao Liu on 13-11-25.
//  Copyright (c) 2013年 Alan. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import <unistd.h>
#import "MBProgressHUD.h"
#import "FridUtil.h"
#import "AppDelegate.h"

@implementation FridUtil
{
    BOOL isConnected;
}
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    
    }
    return self;
}

-(AppDelegate *) getInstance {
	return (AppDelegate *)[[UIApplication sharedApplication]delegate];
}

#pragma mark---Delegate

-(void) readerConnect:(NSString *) deviceSN
{
    NSLog(@"Reader Connect,deviceSN%@",deviceSN);
}
-(void) readerDisconnect
{
    NSLog(@"Reader Disconnect");
    
}
-(void) readerStatusChange: (IvrJackStatus) status
{
    switch (status) {
		case ijsDetecting:
			NSLog(@"Detecting...");
//            [MBProgressHUD showHUDAddedTo:self animated:YES];
            UIAlertView *alert2 = [[UIAlertView alloc]initWithTitle:@"提示" message:@"设备正在连接中。。。。" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert2 show];
			break;
		case ijsRecognized:
			NSLog(@"Recognized.") ;
//            [MBProgressHUD hideHUDForView:self animated:YES];
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"设备已连接！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
//            isConnected = 1;
			break;
		case ijsUnRecognized:
			NSLog(@"Unrecognized!") ;
            UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"警告！！" message:@"设备无法识别！！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            isConnected = 0;
            [alert1 show];
			break;
		case ijsPlugout:
			NSLog(@"Plugout!")  ;
            UIAlertView *alert3 = [[UIAlertView alloc]initWithTitle:@"警告！！" message:@"设备已拔出！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            isConnected = 0;
            [alert3 show];
			break;
		default:
			break;
	}
}
-(void)doSomething:(NSNotification*)send
{
   
}
-(void)onReadTag:(NSString *)epc
{
    NSLog(@"%@",epc);
//    if ([NSNotificationCenter defaultCenter]) {
//        <#statements#>
//    }
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"frid" object:nil];
//self notification:<#(NSTimer *)#>
    [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(notification:) userInfo:epc repeats:NO];
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(doSomething:) name:@"frid" object:nil];
//    [[NSNotificationCenter defaultCenter]postNotificationName:@"frid" object:[NSString stringWithFormat:@"%@",epc]];
}
-(void)notification:(NSTimer*)send
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(doSomething:) name:@"frid" object:nil];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"frid" object:[NSString stringWithFormat:@"%@",[send userInfo]]];
    NSLog(@"%@",[send userInfo]);
}
- (void)readData {
    [self getInstance].reader.isCancel = NO;
	[self performSelectorInBackground: @selector(readTagInfo) withObject: nil];
}
- (void)closeRead
{
//    [self ];
}
-(void) readTagInfo {
    int ret = [((AppDelegate*)[[UIApplication sharedApplication] delegate]).reader readEPC: YES];
    int newRet =[((AppDelegate*)[[UIApplication sharedApplication] delegate]).reader setBuzzer:YES];
    [self performSelectorOnMainThread:@selector(doMyJob:) withObject:[NSString stringWithFormat:@"%d",ret] waitUntilDone:NO];

    NSLog(@"%d %d",ret,newRet);
	if (ret == 0) {
		NSLog(@"Read success.");
	}
	else {
		NSLog(@"Device communication error, make sure that is plugged.");
	}
}
-(void)doMyJob:(NSString*)send
{
    NSString *message =nil;
    NSArray *messageArray =[[NSArray alloc]initWithObjects:@"正在清点EPC,不能进行此操作",@"frid设备未连接",@"frid设备电量不足",@"frid设备连接成功",@"frid设备通讯失败",@"frid设备未知的异常错误", nil];
    if (send.intValue<200) {
        message = [messageArray objectAtIndex:send.intValue+3];
    }
    else
    {
        message =@"frid设备 标签读取失败!";
    }
    if (!isConnected)
    {
        UIAlertView *alertVieww =[[UIAlertView alloc]initWithTitle:nil message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alertVieww show];
        [alertVieww release];
        isConnected = 1;
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"frid" object:nil];
    [super dealloc];
}
@end
